package com.atguigu.controller;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.vod.model.v20170321.DeleteVideoRequest;
import com.aliyuncs.vod.model.v20170321.DeleteVideoResponse;
import com.atguigu.commonutils.R;
import com.atguigu.service.VodService;
import com.atguigu.servicebase.handler.MyException;
import com.atguigu.utils.AliyunVideoConstont;
import com.atguigu.utils.InitVodClient;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;

/**
 * @Auther: fangshicai
 * @Date: 2022/2/25 16:09
 * @Description:
 */
@RestController
@RequestMapping("/eduvod/video")
@CrossOrigin
@Api(description="阿里云视频管理")
public class VodUploadController {

    @Autowired
    private VodService vodService;


    /**
     *上传视频到阿里云
     */
    @PostMapping("uploadvideoaliyun")
    public R uploadVideoAliyun(
             MultipartFile file){
        String videoId = vodService.uploadAliyunVideo(file);
        System.out.println(videoId);
        return R.ok().data("videoId",videoId);
    }

    /**
     *根据id删除阿里云的上传视频
     */
    @DeleteMapping("deleteAliyunVideo/{id}")
    public R deleteAliyunVideo(@PathVariable String id){
        try {
            //1.创建初始化对象
            DefaultAcsClient defaultAcsClient = InitVodClient.initVodClient(AliyunVideoConstont.ACCESS_KEY_ID,AliyunVideoConstont.ACCESS_KEY_SECRET);
            //2.创建request
            DeleteVideoRequest request = new DeleteVideoRequest();
            //3.创建request设置视频id
            request.setVideoIds(id);
            //4.创建response
            //5.删除
            defaultAcsClient.getAcsResponse(request);
            return R.ok();
        } catch (ClientException e) {
            e.printStackTrace();
            throw  new MyException(20001,"删除失败");
        }

    }
    /**
     *根据传入的id集合删除阿里云的上传视频
     */
    @DeleteMapping("deleteAliyunVideo-batch")
    public R deleteAliyunVideoBatch(@RequestParam List<String> videoAliyunSourseIdsList){

        vodService.removeAliyunVideoBatch(videoAliyunSourseIdsList);
        return R.ok();

    }



}
